﻿/*  ----------------------------------------------------------------------------
 *  Warren Moxley
 *  ----------------------------------------------------------------------------
 *  GTasksSync
 *  ----------------------------------------------------------------------------
 *  File:       frmSettings.cs
 *  Author:     IT1SERVICES\wmoxley
 *  ----------------------------------------------------------------------------
	Licensed to the Apache Software Foundation (ASF) under one
       	or more contributor license agreements.  See the NOTICE file
       	distributed with this work for additional information
       	regarding copyright ownership.  The ASF licenses this file
       	to you under the Apache License, Version 2.0 (the
       	"License"); you may not use this file except in compliance
       	with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

       	Unless required by applicable law or agreed to in writing,
       	software distributed under the License is distributed on an
       	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       	KIND, either express or implied.  See the License for the
       	specific language governing permissions and limitations
       	under the License. 
 *  ----------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utility.ModifyRegistry;
using Microsoft.Win32;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace GTasksSync
{
    public partial class frmSettings : Form
    {
        private string userName;
        private string password;
        private int refreshMinutes;
        private string seedKey;
        private Outlook.Application application;

        public frmSettings(Outlook.Application application, string seedKey)
        {
            InitializeComponent();

            this.application = application;
            this.seedKey = seedKey;
            loadConfig();

            bDirty = false;

            refreshUI();

        }
        
        private void loadConfig()
        {
            ModifyRegistry reg = new ModifyRegistry();

            reg.BaseRegistryKey = Registry.CurrentUser;

            try
            {
                string strProfileName = application.Session.CurrentProfileName;

                if (string.IsNullOrEmpty(strProfileName))
                    strProfileName = "Default";

                ddlRefreshMin.Items.Clear();

                for (int i = 1440; i >= 0; i--)
                {
                    ddlRefreshMin.Items.Add(i);
                }

                try
                {
                    userName = Util.Encryption.DecryptString(reg.Read(strProfileName + "-gUser"), seedKey);
                    password = Util.Encryption.DecryptString(reg.Read(strProfileName + "-gPass"), seedKey);
                }
                catch (Exception ex) {
                    userName = "";
                    password = "";
                }

                refreshMinutes = 0;

                int.TryParse(reg.Read(strProfileName + "-gRefreshMin"), out refreshMinutes);

                if (!string.IsNullOrEmpty(userName))
                    txtUsername.Text = userName;

                if (!string.IsNullOrEmpty(password))
                    txtPassword.Text = password;

                ddlRefreshMin.SelectedItem = refreshMinutes;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Occurred: " + ex.Message + Environment.NewLine + "Closing...");
                this.Close();

            }

        }
        
        bool bDirty = false;

        void saveConfig()
        {
            try
            {
                ModifyRegistry reg = new ModifyRegistry();

                reg.BaseRegistryKey = Registry.CurrentUser;

                string strProfileName = application.Session.CurrentProfileName;

                reg.Write(strProfileName + "-gUser", Util.Encryption.EncryptString(txtUsername.Text, seedKey));
                reg.Write(strProfileName + "-gPass", Util.Encryption.EncryptString(txtPassword.Text, seedKey));
                reg.Write(strProfileName + "-gRefreshMin", ddlRefreshMin.Text);

                bDirty = false;

            }
            catch (Exception ex)
            {
                bDirty = true;
            }

            refreshUI();
            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (bDirty)
                    saveConfig();

                this.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show("Error Saving Configuration: " + ex.Message);
                
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        void refreshUI()
        {
            btnApply.Enabled = bDirty;
            
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (bDirty)
                    saveConfig();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Saving Configuration: " + ex.Message);
            }

        }

        private void txtBox_TextChanged(object sender, EventArgs e)
        {
            var tb = sender as TextBox;

            if(tb != null)
                if(string.IsNullOrEmpty(tb.Text))
                {
                    int iRefreshMin = 0;

                    if (int.TryParse(ddlRefreshMin.Text, out iRefreshMin) && iRefreshMin > 0)
                    {
                        MessageBox.Show("You must enter a username and password.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        tb.Focus();
                        return;
                    }
                }

            bDirty = true;
            refreshUI();

        }

        private void ddlRefreshMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            int isNumber = 0; 
            
            e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);

        }

    }

}
